<script setup>
import { ref, watch, onMounted } from 'vue'
import { User, Lock, View } from '@element-plus/icons-vue'
import { userRegisterService, userLoginService } from '@/api/user.js'
import { useUserStore } from '@/stores/index.js'
import { useRouter } from 'vue-router'

const isRegister = ref(false)

const form = ref()
const userStore = useUserStore()

// 用于提交的form数据
const formModel = ref({
  username: '',
  password: '',
  repassword: '',
})

const loadRememberInfo = () => {
  formModel.value.username = userStore.rememberInfo.username
  formModel.value.password = userStore.rememberInfo.password
}

// 注册
const register = async () => {
  // 校验是否正确
  await form.value.validate()
  // 提交给后端
  const res = await userRegisterService(formModel.value)
  ElMessage.success('注册成功')
  isRegister.value = false
}

const router = useRouter()
const login = async () => {
  await form.value.validate()
  // 获取登录码，存储
  const res = await userLoginService(formModel.value.username, formModel.value.password)
  console.log('开始登陆', res)
  await userStore.setToken(res.data.access)
  await userStore.renewInfo()
  ElMessage.success('登录成功')
  userStore.storeRememberInfo(formModel.value.username, formModel.value.password)
  router.push('/')
}
const rules = {
  username: [
    { required: true, message: '用户名不能为空', trigger: 'blur' },
    { min: 3, max: 10, message: '长度应该在3到10个字符之间', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '密码不能为空', trigger: 'blur' },
    { pattern: /^\S{6,15}$/, message: '密码必须是6-15位非空字符', trigger: 'blur' },
  ],
  repassword: [
    { required: true, message: '再次输入密码不能为空', trigger: 'blur' },
    { pattern: /^\S{6,15}$/, message: '密码必须是6-15位非空字符', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        if (value !== formModel.value.password) {
          return callback(new Error('与原密码不一致'))
        } else {
          return callback()
        }
      },
      trigger: 'blur',
    },
  ],
}

watch(isRegister, () => {
  formModel.value = {
  username: '',
  password: '',
  repassword: '',
  }
})

onMounted(async () => {
  loadRememberInfo()
})

</script>

<template>
  <el-row>
    <el-col :span="12">
      <img src="/src/assets/sence.jpg" alt="" style="width: 100%" />
    </el-col>

    <el-col :span="6" :offset="3">
      <!-- 注册 -->
      <el-form
        :model="formModel"
        :rules="rules"
        ref="form"
        size="large"
        autocomplete="off"
        v-if="isRegister"
      >
        <el-form-item>
          <h1>注册</h1>
        </el-form-item>
        <el-form-item prop="username">
          <el-input :prefix-icon="User" placeholder="请输入用户名" v-model="formModel.username" />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            name="password"
            :prefix-icon="Lock"
            type="password"
            placeholder="请输入密码"
            v-model="formModel.password"
          />
        </el-form-item>
        <el-form-item prop="repassword">
          <el-input
            name="repassword"
            :prefix-icon="View"
            type="password"
            placeholder="请再次输入密码"
            v-model="formModel.repassword"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            size="large"
            auto-insert-space
            style="width: 100%"
            @click="register"
            >注册</el-button
          >
        </el-form-item>
        <el-form-item>
          <el-link type="info" @click='isRegister=false'>登录-></el-link>
        </el-form-item>
      </el-form>

      <!-- 登录 -->
      <el-form ref="form" size="large" autocomplete="off" v-else :model="formModel" :rules="rules">
        <el-form-item>
          <h1>登录</h1>
        </el-form-item>
        <el-form-item prop="username">
          <el-input v-model="formModel.username" :prefix-icon="User" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            name="password"
            :prefix-icon="Lock"
            type="password"
            placeholder="请输入密码"
            v-model="formModel.password"
          />
        </el-form-item>
        <el-form-item>
          <el-checkbox v-model="userStore.remember" label="记住密码" size="large" />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            size="large"
            auto-insert-space
            style="width: 100%"
            @click="login"
            >登录</el-button
          >
        </el-form-item>
        <el-form-item>
          <el-link type="info" @click='isRegister=true' >注册-></el-link>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>

<style scoped>
.el-row {
  height: 100vh;
  display: flex;
  align-items: center;
  background-color: #f5f7fa;
}

h1 {
  color: #409eff;
  text-align: center;
  margin-bottom: 30px;
  font-size: 24px;
}

.el-link {
  display: block;
  text-align: center;
  margin-top: 10px;
}

.el-checkbox {
  margin-left: 0;
}

/* 响应式调整 */
@media (max-width: 768px) {
  .el-col {
    width: 90%;
    margin: 0 auto;
  }

  .el-row {
    display: block;
    padding-top: 20px;
  }

  img {
    display: none;
  }
}
</style>

